import json
import pandas as pd
import pymongo
import requests
from get_field_name.config import *
from get_field_name.save_to_db import *
# 连接数据库
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
table = db['second_name']

def get_second_field_name_list():
    data = pd.DataFrame(list(table.find()))
    data = data[["id", "n", "first_field_name"]]
    second_field_name_list = []
    for i in data.index:
        second_field_name_list.append({"id": data.loc[i][0], "second_name": data.loc[i][1], "first_name": data.loc[i][2]})
    # for n in second_field_name_list:
    #     print(n)
    return second_field_name_list

def get_third_field_name(url):
    try:
        response = requests.get(url)
        if response.content:  # 返回成功
            return response.content
    except requests.ConnectionError as e:
        print('url出错', e.args)



if __name__ == '__main__':
    second_field_name_list = get_second_field_name_list()
    for second in second_field_name_list:
        url = "https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=" + str(second['id'])
        print(second['second_name'])
        third_filed_name = json.loads(get_third_field_name(url))['ct']
        parent_field_name = json.loads(get_third_field_name(url))['bc']
        # for pp in parent_field_name:
        #     print(pp)
        for ss in third_filed_name:
            ss['first_field_name'] = second['first_name']
            ss['second_field_name'] = parent_field_name[0]
        save_data("third_name", third_filed_name)